<template>
  <v-card :title="title" @submit="submit" @reset="reset">
    <v-form
      slot="form"
      :form-title="formTitle"
      :form-data="formData"

    />
  </v-card>
</template>

<script>
export default {
  data() {
    return {
      id: '',
      title: '编辑',
      formData: {},
      permissionList: [],
      menuList: []
    }
  },
  computed: {
    formTitle() {
      return ([
        {
          cn: 'id',
          en: 'id',
          disabled: true
        },
        {
          en: 'slug',
          cn: '标识'
        },
        {
          en: 'name',
          cn: '名称'
        },
        {
          en: 'PermissionsIdList',
          cn: '权限',
          type: 'checkbox',
          list: this.permissionList
        },
        {
          cn: '创建时间',
          en: 'created_at',
          disabled: true
        },
        {
          cn: '更新时间',
          en: 'updated_at',
          disabled: true
        }
      ])
    }
  },
  activated() {
    // 接收传参
    this.id = this.$route.query.id
    const details = this.$route.params.details

    if (details) {
      localStorage.setItem('details', JSON.stringify(details))
      this.formData = this.$route.params.details
    }

    this.formData = localStorage.getItem('details') && JSON.parse(localStorage.getItem('details')) || {}

    // this.getMenu();
    this.$emit('childTitle', this.$route.meta.title1)
    this.getPermissionList()
  },

  methods: {

    // 获取角色对应权限列表
    getRole() {
      const data = { id: this.id }
      this.$get('action/Permissions/getRoleToPermissions', data)
        .then(res => {
          const list = res.data.map(item => {
            return item.permission_id
          }) || []
          this.$set(this.formData, 'PermissionsIdList', list || [])
        })
    },

    // 获取所有权限列表
    getPermissionList() {
      this.$get('action/Permissions/dataGrid').then(res => {
        this.permissionList = res.data.map(item => {
          return {
            label: item.name,
            value: item.id
          }
        }) || []

        this.getRole()
      })
    },
    submit() {
      this.formData.PermissionsIdList = this.formData.PermissionsIdList.toString()
      // this.formData.menuIdList = this.formData.menuIdList.toString();
      const data = this.formData
      this.$post('action/PositionCtrl/save', data)
        .then(res => {
          this.getRole()
          this.$message({
            message: res.msg,
            type: 'success'
          })
        })
    },
    reset() {
      this.formData = localStorage.getItem('details') && JSON.parse(localStorage.getItem('details')) || {}

      this.getRole()
    }
  }
}
</script>

<style scoped>

</style>
